"""
---------------------------------------------------------------------
- File: logs.py
- Date: 2025/11/19
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 日志模型
---------------------------------------------------------------------
"""

import uuid
from typing import List

from pydantic import computed_field
from sqlmodel import SQLModel, Field

from app.schemas.sqlmode_base import IdTimeModel
from app.schemas.system.users import UserRead


class LogsBase(SQLModel):
    request_id: uuid.UUID | None = Field(default="请求唯一ID")
    ip: str | None = Field(default=None, description="请求IP")
    url: str | None = Field(None, description="请求URL")
    method: str | None = Field(None, description="请求方法")
    path_params: dict | None = Field(None, description="路径参数")
    query_params: dict | None = Field(None, description="查询参数")
    requst_body: dict | None = Field(None, description="请求体")
    status_code: int | None = Field(description="响应状态码")
    response_body: dict | None = Field(None, description="响应体")
    process_time: float | None = Field(description="请求耗时（ms）")


class LogsCreate(LogsBase):
    pass


class LogsUpdate(LogsBase):
    pass


class LogsRead(IdTimeModel, LogsBase):
    request_id: uuid.UUID = Field(default="请求唯一ID")
    ip: str | None = Field(default=None, description="请求IP")
    url: str = Field(description="请求URL")
    method: str = Field(description="请求方法")
    path_params: dict | None = Field(None, description="路径参数")
    query_params: dict | None = Field(None, description="查询参数")
    requst_body: dict | None = Field(None, description="请求体")
    status_code: int | None = Field(description="响应状态码")
    response_body: dict | None = Field(None, description="响应体")
    process_time: float | None = Field(description="请求耗时（ms）")


class LogsReadWithUser(LogsRead):
    # 如果需要模型中的字段，需要添加模型，之后供@computed_field, @property使用
    user: UserRead | None = Field(None, description="用户信息", exclude=True)


class LogsReadWithUserName(LogsReadWithUser):

    @computed_field
    @property
    def user_name(self) -> str | None:
        if self.user:
            return self.user.user_name
        return None


class LogsReadWithUserAndUserName(IdTimeModel):
    request_id: uuid.UUID = Field(default="请求唯一ID")
    ip: str | None = Field(default=None, description="请求IP")
    url: str = Field(description="请求URL")
    method: str = Field(description="请求方法")
    path_params: dict | None = Field(None, description="路径参数")
    query_params: dict | None = Field(None, description="查询参数")
    requst_body: dict | None = Field(None, description="请求体")
    status_code: int | None = Field(description="响应状态码")

    user: UserRead | None = Field(None, description="用户信息", exclude=True)

    @computed_field
    @property
    def user_name(self) -> str | None:
        if self.user:
            return self.user.user_name
        return None


class LogsReadListWithUserAndUserName(SQLModel):
    list: List[LogsReadWithUserAndUserName] = Field(default_factory=list, description="日志列表")
    total: int = Field(default=0, description="日志总数")
